const db=wx.cloud.database()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:"",
    list:""
  },
  fun1(){
    wx.navigateTo({
      url: '/packageA/pages/test/test',
    })
  },
  tiaozhuanshoucang(){
    wx.navigateTo({
      url: '/packageA/pages/shoucang/shoucang',
    })
  },
  tiaozhuanguanzhu(){
    wx.navigateTo({
      url: '/packageA/pages/guanzhu/guanzhu',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const allid=wx.getStorageSync('id') 
    this.setData({
      id:options.id||allid||"vinSur9aoaEQydwf"
    })
    this.getdata()
  },

getdata(){
  db.collection("student").doc(this.data.id).get().then(res=>{
    this.setData({
      list:res.data
    })
  })
},


  fun(){
    wx.navigateTo({
      url: '/packageA/pages/huodongnum/huodongnum?id='+this.data.id,
    })
  },
  makePhoneCall(){
    const phoneNumber = '18671931208'; // 替换成要拨打的电话号码
    wx.makePhoneCall({
      phoneNumber: phoneNumber
    })
  },
  tiaozhuan3(){
    this.makePhoneCall()
  },


  tiaozhuan4(){
    wx.navigateTo({
      url: '/packageA/pages/feedback/feedback',
    })
  },
  tiaozhuan5(){
    wx.navigateTo({
      url: '/packageA/pages/xuexijilu/xuexijilu',
    })
  },
  // imageClick(event){
  //  let sid=this.data.id
  //   wx.chooseMedia({
  //     count: 1,
  //     mediaType: ['image','video'],
  //     sourceType: ['album', 'camera'],
  //     maxDuration: 30,
  //     camera: 'back',
  //     success(res) {
  //       // console.log(res.tempFiles[0].tempFilePath)
  //       // console.log(res.tempFiles[0].size)
  //       console.log(res);
  //       let filePath = res.tempFiles[0].tempFilePath
  //       console.log(filePath);
  //       db.collection("student").doc(sid).update({
  //         data:{
  //           avater:filePath
  //         }
  //       }).then(res=>{
  //         console.log(res);
  //         wx.showToast({
  //           title: '更换成功',
  //         })
  //     })
  //     }
  //   })
    
  // },
  chooseMedia() {
    return new Promise((resolve, reject) => {
      wx.chooseMedia({
        count: 1,
        mediaType: ['image', 'video'],
        sourceType: ['album', 'camera'],
        maxDuration: 30,
        camera: 'back',
        success(res) {
          resolve(res.tempFiles[0].tempFilePath);
        },
        fail(error) {
          reject(error);
        }
      });
    });
  },
  cloudId2Http(cloudId, styleName) {
    if (!cloudId) return "请传入cloudID";
    const strs = cloudId.split(".");
    const strs_2 = strs[1];
    const strs_3 = strs_2.split("/");
    const envId = strs_3[0];
    const https_prefix = `https://${envId}.tcb.qcloud.la/`;
    let filePath = "";
    for (let i = 1; i < strs_3.length; i++) {
      if (i === strs_3.length - 1) {
        const fileName = strs_3[i] + "." + strs[strs.length - 1];
        filePath += fileName;
      } else {
        filePath += strs_3[i] + "/";
      }
    }
    let httpsUrl = https_prefix + filePath;
    if (styleName) httpsUrl += "/" + styleName;
    return httpsUrl;
  },
 updateAvatar(filePath) {
    let sid = this.data.id;
    this.cloudId2Http(filePath)
    return new Promise((resolve, reject) => {
      db.collection("student").doc(sid).update({
        data: {
          avater: filePath
        }
      }).then(res => {
        resolve(res);
      }).catch(error => {
        reject(error);
      });
    });
  },
  
 getData(){
     let sid = this.data.id;
    return new Promise((resolve, reject) => {
      db.collection("student").doc(sid).get().then(res => {
        resolve(res.data);
      }).catch(error => {
        reject(error);
      });
    });
  },
  imageClick(event) {
    this.chooseMedia()
      .then(filePath => {
        return this.updateAvatar.call(this, filePath);
      })
      .then(() => {
        wx.showToast({
          title: '更换成功',
        });
        // 在更新头像成功后执行获取数据操作
        return this.getData.call(this);
      })
      .then(data => {
        this.setData({
          list: data
        });
      })
      .catch(error => {
        console.error(error);
      });
  },
 

  tiaozhuan6(){
    wx.removeStorage({
      key: 'id',
      success (res) {
        console.log(res)
      }
    })

    wx.reLaunch({
      url: '/pages/index/index',
    })
  },
 

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getdata()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})